Completed
Push — master ( 7de168...59e781 )
by
unknown
04:23
created

get-main.js ➔ route   C

Complexity

Conditions 7
Paths 3078

Size

Total Lines 119

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
c 3
b 0
f 0
nc 3078
dl 0
loc 119
rs 5.3042
nop 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A get-main.js ➔ ... ➔ ?!?.catch 0 3 1
B get-main.js ➔ ... ➔ ??? 0 30 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import xss from 'xss'
2
import path from 'path'
3
import pkg from '../../../package'
4
5
import {
6
  config,
7
  Page,
8
  cmsData,
9
  cmsTemplates,
10
  coreUtils,
11
  abeExtend,
12
  Manager
13
} from '../../cli'
14
15
import {editor} from '../controllers/editor'
16
import locale from '../helpers/abe-locale'
17
18
var route = function(req, res, next) {
19
  var filePath = req.originalUrl.replace('/abe', '')
20
  if (filePath === '' || filePath === '/') {
21
    filePath = null
22
  }
23
24
  if(filePath != null){
25
    var testXSS = xss(filePath, {
26
      whiteList: [],
27
      stripIgnoreTag: true
28
    })
29
    if(testXSS !== filePath){
30
      filePath = testXSS
31
    }
32
  }
33
34
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
35
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
36
37
  var isHome = true
38
  var jsonPath = null
39
  var linkPath = null
40
  var templatePath = null
41
42
  let p = new Promise((resolve) => {
43
44
    if(filePath != null) {
45
      isHome = false
46
47
      var filePathTest = cmsData.revision.getDocumentRevision(filePath)
48
      if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
49
        jsonPath = filePathTest.path
50
        linkPath = filePathTest.abe_meta.link
51
        templatePath = filePathTest.abe_meta.template
52
      }
53
54
      if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { 
55
        res.redirect('/abe/') 
56
        return 
57
      }
58
59
      editor(templatePath, jsonPath, linkPath)
60
        .then((result) => {
61
          resolve(result)
62
        }).catch(function(e) {
63
          console.error(e)
64
        })
65
    }else {
66
      resolve({
67
        obj: {},
68
        manager: {}
69
      })
70
    }
71
  }).catch(function(e) {
72
    console.error(e) // "oh, no!"
73
  })
74
75
  p.then((result) => {
76
    var obj = result
77
    var manager = {}
78
  
79
    manager.home = {
80
      files: Manager.instance.getList()
81
    }
82
83
    manager.list = Manager.instance.getStructureAndTemplates()
84
    manager.editConfig = req.app.get('config')
85
    manager.config = JSON.stringify(config)
86
    
87
    var _hasBlock = (obj) ? obj.hasBlock : false
88
    var _hasSingleBlock = (obj) ? obj.hasSingleBlock : false
89
    var _template = (filePath) ? '/abe/page/' + req.params[0] + `?filePath=${req.query.filePath}` : false
90
    var _form = (obj) ? obj.form : false
91
    var _json = (obj) ? obj.json : false
92
    var _text = (obj) ? obj.text : false
0 ignored issues
show
Unused Code introduced by
The variable _text seems to be never used. Consider removing it.
Loading history...
93
    // var _file = (tplUrl) ? tplUrl.draft.file : false
94
    var _filePath = (filePath) ? filePath : false
95
    if (_filePath) {
96
      _filePath = '/' + _filePath.replace(/^\/+/, '')
0 ignored issues
show
Unused Code introduced by
The assignment to variable _filePath seems to be never used. Consider removing it.
Loading history...
97
    }
98
99
    var pageHtml = ''
100
    if(typeof _json !== 'undefined' && _json !== null 
101
      && typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) {
102
103
      var text = cmsTemplates.template.getTemplate(_json.abe_meta.template) 
104
      var page = new Page(_json.abe_meta.template, text, _json, false) 
105
      pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>')
106
    }
107
108
    var EditorVariables = {
109
      pageHtml: pageHtml,
110
      isHome: isHome,
111
      abeUrl: '/abe/',
112
      test: JSON.stringify(locale),
113
      text: locale,
114
      templatePath: req.params[0],
115
      template: _template,
116
      hasSingleBlock: _hasSingleBlock,
117
      hasBlock: _hasBlock,
118
      form: _form,
119
      json: _json,
120
      config: config,
121
      Locales: coreUtils.locales.instance.i18n,
122
      manager: manager,
123
      express: {
124
        res: res,
125
        req: req
126
      },
127
      abeVersion: pkg.version,
128
      nonce: '\'nonce-' + res.locals.nonce + '\''
129
    }
130
    EditorVariables = abeExtend.hooks.instance.trigger('afterVariables', EditorVariables)
131
132
    res.render(config.abeEngine, EditorVariables)
133
  }).catch((e) => {
134
    console.log('error', e)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
135
  })
136
}
137
138
export default route